package service

import (
	"context"
	"rpc-server/basic/config"
	__ "rpc-server/basic/proto"
	"rpc-server/handler/model"
)

// server is used to implement helloworld.GreeterServer.
type Server struct {
	__.UnimplementedServerServer
}

// SayHello implements helloworld.GreeterServer
func (s *Server) UploadVideo(_ context.Context, in *__.UploadVideoReq) (*__.UploadVideoResp, error) {
	video := model.Video{Url: in.File}
	if err := config.DB.Create(&video).Error; err != nil {
		return &__.UploadVideoResp{
			Code: 500,
			Msg:  "failed",
		}, err
	}
	return &__.UploadVideoResp{
		Code: 200,
		Msg:  "success",
	}, nil
}

func (s *Server) AddVideo(_ context.Context, in *__.AddVideoReq) (*__.AddVideoResp, error) {
	video := model.Video{
		Title:       in.Title,
		Description: in.Description,
		Url:         in.Url,
		Category:    in.Category,
		UserId:      uint(in.UserId),
	}
	if err := config.DB.Create(&video).Error; err != nil {
		return &__.AddVideoResp{
			Code: 500,
			Msg:  "failed",
		}, err
	}
	return &__.AddVideoResp{
		Code: 200,
		Msg:  "success",
	}, nil
}

func (s *Server) ListVideo(_ context.Context, in *__.ListVideoReq) (*__.ListVideoResp, error) {
	var videos []model.Video
	if err := config.DB.Find(&videos).Error; err != nil {
		return &__.ListVideoResp{
			Code: 500,
			Msg:  "failed",
		}, err
	}
	//model.Paginate(in.Page, in.PageSize, &videos)
	return &__.ListVideoResp{
		Code: 200,
		Msg:  "success",
	}, nil
}
